"""
Case Type   : cm
Case Name   : cm_ctl view查看数据库实例配置文件基本参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、查看数据库实例配置文件
    3、查看数据库实例配置文件，指定-v
    4、查看数据库实例配置文件，指定-N
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、成功
    3、成功，显示详细信息（增加了cmserver、节点组件的编号）
    4、成功（只显示本节点的静态配置信息）
History     :
"""

import os
import re
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:查看数据库实例配置文件; expect:成功----'
        self.log.info(text)

        view_list1 = []
        res1 = self.comsh.exec_cm_ctl('view')
        for view in res1.splitlines():
            view_list1.append(view)
        self.log.info(view_list1)
        #self.assertTrue(re.match('node.*',view_list1[9]), '节点信息查询失败')

        text = '----step3:查看数据库实例配置文件，指定-v  expect:成功，显示详细信息（增加了cmserver、节点组件的编号）----'
        self.log.info(text)

        view_list2 = []
        res2 = self.comsh.exec_cm_ctl('view', '-v')
        for view in res2.splitlines():
            view_list2.append(view)
        self.log.info(view_list2)
        #self.assertTrue(re.match('datanodeInstanceID.*',view_list2[9]), '节点信息查询失败')

        text = '----step4:查看数据库实例配置文件，指定-N  expect:成功（只显示本节点的静态配置信息）----'
        self.log.info(text)

        view_list3 = []
        res3 = self.comsh.exec_cm_ctl('view', '-N')
        for view in res3.splitlines():
            view_list3.append(view)
        self.log.info(res3)
        #self.assertTrue(re.match('datanodeInstanceID.*',view_list3[9]), '节点信息查询失败')


    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
